package com.skytech.component.crawler.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.parse.ParserLoader;
import com.skytech.component.crawler.scratch.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

import java.util.List;

@Service
public class CrawlerTask {
    @Autowired
    private CrawlerUrlDAO crawlerUrlDAO;
    @Autowired
    private TargetRulesDAO targetRulesDAO;
    @Autowired
    private ParserLoader parserLoader;
    @Autowired
    private PageContentDAO pageContentDAO;
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;

    @Value("${elasticsearch.server}")
    private String server;

    @Value("${chrome.driver.path}")
    private String driverPath;

    private static final Logger log = LoggerFactory.getLogger(CrawlerTask.class);

    public static Spider spider;



    public void startChromeTask(){
        List<TargetRules> ruleList = targetRulesDAO.findAll();
        TaskManager taskManager = new TaskManager(ruleList,driverPath,new NewsListPageOperation(crawlerUrlDAO));
        taskManager.start();
        //todo parse
        parserLoader.load();

    }
    public void backUpData(){
        List<PageContent> list = pageContentDAO.findAll();
        for (PageContent pageContent : list) {
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(pageContent));
            jsonObject.remove("id");
            restTemplateBuilder.build().put(server+"crawler_news/news/"+pageContent.getId(),jsonObject);
        }
    }
}
